package com.qfedu.labsystem.config;

import com.baomidou.mybatisplus.core.handlers.MetaObjectHandler;
import org.apache.ibatis.reflection.MetaObject;
import org.springframework.stereotype.Component;

import java.util.Date;
import java.util.Objects;

/**
 * @ClassName MyMetaObjHandler
 * @Author ykx
 * @Date 2024/4/8 22:13
 * @Version 1.0
 */
@Component
public class MyMetaObjHandler implements MetaObjectHandler {
    @Override
    public void insertFill(MetaObject metaObject) {
        Object creationDate = getFieldValByName("creationDate", metaObject);
        if (Objects.isNull(creationDate)) {
            this.strictInsertFill(metaObject, "creationDate", Date.class, new Date());
        }

        Object lastUpdateDate = getFieldValByName("lastUpdateDate", metaObject);
        if (Objects.isNull(lastUpdateDate)) {
            this.strictInsertFill(metaObject, "lastUpdateDate", Date.class, new Date());
        }

        Object deleteFlag = getFieldValByName("deleteFlag", metaObject);
        if (Objects.isNull(deleteFlag)) {
            this.strictInsertFill(metaObject, "deleteFlag", Integer.class, 0);
        }

        Object createBy = getFieldValByName("createBy", metaObject);
        if (Objects.isNull(createBy)) {
            // todo:fieldVal需要换成当前登录人id
            this.strictInsertFill(metaObject, "createBy", Long.class, 0L);
        }

        Object lastUpdateBy = getFieldValByName("lastUpdateBy", metaObject);
        if (Objects.isNull(lastUpdateBy)) {
            // todo:fieldVal需要换成当前登录人id
            this.strictInsertFill(metaObject, "lastUpdateBy", Long.class, 0L);
        }

    }

    @Override
    public void updateFill(MetaObject metaObject) {
        Object lastUpdateBy = getFieldValByName("lastUpdateBy", metaObject);
        if (Objects.isNull(lastUpdateBy)) {
            // todo:fieldVal需要换成当前登录人id
            this.strictInsertFill(metaObject, "lastUpdateBy", Long.class, 0L);
        }
        Object lastUpdateDate = getFieldValByName("lastUpdateDate", metaObject);
        if (Objects.isNull(lastUpdateDate)) {
            this.strictInsertFill(metaObject, "lastUpdateDate", Date.class, new Date());
        }
    }
}
